package mess;

public class LeeCode_1694 {
    public static void main(String[] args) {
        System.out.println(reformatNumber("789-0469-2731  5107289"));
    }
    private static String reformatNumber(String number) {
        StringBuilder newNumBuilder = new StringBuilder();
        for (int i = 0; i < number.length(); i++) {
            char c = number.charAt(i);
            if (c != ' ' && c != '-')
                newNumBuilder.append(c);
        }
        int len = newNumBuilder.length();
        int count = len / 3;
        int remain = len % 3;
        if (remain == 0){
            for (int i = 1; i < count; i++)
                newNumBuilder.insert(i * 3 + i - 1, '-');
        }else if (remain == 1){
            for (int i = 1; i < count; i++)
                newNumBuilder.insert(i * 3 + i - 1, '-');
            newNumBuilder.insert(count * 3 + count - 2, '-');
        }else if (remain == 2){
            for (int i = 1; i <= count; i++)
                newNumBuilder.insert(i * 3 + i - 1, '-');
        }
        return newNumBuilder.toString();
    }
}
